/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import view.admin.AdministratorManage;
import dto.EmployeeDto;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import view.employee.EmployeeManage;
import view.finance.FinanceManage;
import view.manager.Manager;

/**
 *
 * @author khangpm
 */
public class MainFrame extends javax.swing.JFrame {
    EmployeeDto employeeDto;
    /**
     * Creates new form MainFrame
     */
    public MainFrame(EmployeeDto empDto) {
        initComponents();
        this.setLocationRelativeTo(null);
        employeeDto =  empDto;
        
        lblFullName.setText(employeeDto.getEmployeeName());
        lblBirthday.setText(employeeDto.getBirthday().toString());
        lblEmail.setText(employeeDto.getEmailID());
        lblDepartment.setText(employeeDto.getDepartmentName());
        if(employeeDto.getImage() != null && !employeeDto.getImage().equals("")){
            setPicture(lblAvata,employeeDto.getImage());
        }
        this.displayPanel(employeeDto.getRoleName());
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        pnlInformation = new javax.swing.JPanel(){
            @Override
            public void paintComponent(Graphics g)
            {
                try {
                    Dimension d = getSize();
                    Image img = ImageIO.read(getClass().getResource("/images/background1.jpg"));
                    g.drawImage(img, 0, 0, d.width, d.height, this);
                    setOpaque(false);  // lam trong suot
                    super.paintComponent(g);
                } catch (IOException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        lblAvata = new javax.swing.JLabel();
        btnLogout = new javax.swing.JButton();
        lblFullName1 = new javax.swing.JLabel();
        lblBirthday1 = new javax.swing.JLabel();
        lblEmail1 = new javax.swing.JLabel();
        lblDept1 = new javax.swing.JLabel();
        lblFullName = new javax.swing.JLabel();
        lblBirthday = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        lblDepartment = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        pnlControl = new javax.swing.JPanel(){
            @Override
            public void paintComponent(Graphics g)
            {
                try {
                    Dimension d = getSize();
                    Image img = ImageIO.read(getClass().getResource("/images/background5.jpg"));
                    g.drawImage(img, 0, 0, d.width, d.height, this);
                    setOpaque(false);  // lam trong suot
                    super.paintComponent(g);
                } catch (IOException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        jPanel4 = new javax.swing.JPanel(){
            @Override
            public void paintComponent(Graphics g)
            {
                try {
                    Dimension d = getSize();
                    Image img = ImageIO.read(getClass().getResource("/images/background1.jpg"));
                    g.drawImage(img, 0, 0, d.width, d.height, this);
                    setOpaque(false);  // lam trong suot
                    super.paintComponent(g);
                } catch (IOException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Budget Approval System");
        setBackground(new java.awt.Color(255, 255, 255));
        setBounds(new java.awt.Rectangle(0, 0, 1000, 700));
        setMinimumSize(new java.awt.Dimension(1000, 700));
        setPreferredSize(new java.awt.Dimension(1000, 700));
        setResizable(false);
        getContentPane().setLayout(null);

        pnlInformation.setBackground(new java.awt.Color(255, 255, 255));
        pnlInformation.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(102, 102, 255)));

        lblAvata.setBackground(new java.awt.Color(204, 204, 255));
        lblAvata.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        btnLogout.setText("Logout");
        btnLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLogoutActionPerformed(evt);
            }
        });

        lblFullName1.setText("FullName       :");

        lblBirthday1.setText("Birthday        :");

        lblEmail1.setText("Email             :");

        lblDept1.setText("Department  :");

        jLabel1.setText("--------------------------------------------------");

        javax.swing.GroupLayout pnlInformationLayout = new javax.swing.GroupLayout(pnlInformation);
        pnlInformation.setLayout(pnlInformationLayout);
        pnlInformationLayout.setHorizontalGroup(
            pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInformationLayout.createSequentialGroup()
                .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlInformationLayout.createSequentialGroup()
                        .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlInformationLayout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(lblAvata, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnlInformationLayout.createSequentialGroup()
                                .addGap(79, 79, 79)
                                .addComponent(btnLogout, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnlInformationLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblFullName1)
                                    .addComponent(lblBirthday1)
                                    .addComponent(lblEmail1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblDept1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblFullName, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(lblDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlInformationLayout.createSequentialGroup()
                                            .addComponent(lblEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(4, 4, 4))))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlInformationLayout.setVerticalGroup(
            pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInformationLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(lblAvata, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(btnLogout, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblFullName1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblFullName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(16, 16, 16)
                .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblBirthday1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblBirthday, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(16, 16, 16)
                .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblEmail1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblEmail, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(12, 12, 12)
                .addGroup(pnlInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblDept1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblDepartment, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(12, 12, 12))
        );

        getContentPane().add(pnlInformation);
        pnlInformation.setBounds(0, 0, 200, 600);

        pnlControl.setBackground(new java.awt.Color(255, 255, 255));
        pnlControl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        getContentPane().add(pnlControl);
        pnlControl.setBounds(200, 0, 800, 600);

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        getContentPane().add(jPanel4);
        jPanel4.setBounds(0, 600, 1000, 60);

        jMenu1.setText("File");

        jMenuItem1.setText("Exit");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("about");
        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void btnLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogoutActionPerformed
        // TODO add your handling code here:
        int choose;
        choose = JOptionPane.showConfirmDialog(null, "Are you sure?", "Logout", JOptionPane.YES_NO_OPTION);
        if(choose == JOptionPane.YES_OPTION){
            employeeDto.clear();
            this.dispose();
            Login dialog = new Login(new javax.swing.JFrame(), true);
            dialog.setVisible(true);
        }
    }//GEN-LAST:event_btnLogoutActionPerformed
    
    private void displayPanel(String roleName){
        if("Employee".equals(roleName)){
            EmployeeManage em = new EmployeeManage();
            em.setBounds(5, 5, 760, 560);
            pnlControl.add(em);
            pnlControl.updateUI();
        }else if("Manager".equals(roleName)){
            Manager mana = new Manager();
            mana.setBounds(5, 5, 760, 560);
            pnlControl.add(mana);
            pnlControl.updateUI();
        }else if("Finance Manager".equals(roleName)){
            FinanceManage finance = new FinanceManage();
            finance.setBounds(5, 5, 760, 560);
            pnlControl.add(finance);
            pnlControl.updateUI();
        }else if("Administrator".equals(roleName)){
           AdministratorManage adminManage = new AdministratorManage(employeeDto);
            adminManage.setBounds(5, 5, 760, 560);
            pnlControl.add(adminManage, null);
            pnlControl.updateUI();
        }
    }
    
   
    
    private void setPicture(JLabel label, String filename){
        try {
            BufferedImage image = ImageIO.read(getClass().getResource(filename));
            int x = label.getSize().width;         
            int y = label.getSize().height;     
            int iy = image.getHeight();                 
            int ix = image.getWidth();                  
            
            int dx = 0;                             
            int dy = 0;                             
            
            if (x / y > ix / iy) {
                dy = y;
                dx = dy* ix / iy;
            } else {
                dx = x;
                dy = dx * iy / ix;
            }
            ImageIcon icon = new ImageIcon(image.getScaledInstance(dx, dy, image.SCALE_SMOOTH));
            label.setIcon(icon);
        } catch (IOException ex) {
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame(new EmployeeDto()).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogout;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel lblAvata;
    private javax.swing.JLabel lblBirthday;
    private javax.swing.JLabel lblBirthday1;
    private javax.swing.JLabel lblDepartment;
    private javax.swing.JLabel lblDept1;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblEmail1;
    private javax.swing.JLabel lblFullName;
    private javax.swing.JLabel lblFullName1;
    private javax.swing.JPanel pnlControl;
    private javax.swing.JPanel pnlInformation;
    // End of variables declaration//GEN-END:variables
}
